 $(function() {
    var thiswinurl = window.location.href,
        hqTaskIdStr = "hqTaskId",
        hqTaskIdIdx = thiswinurl.indexOf(hqTaskIdStr),
        hqTaskIdVal = (hqTaskIdIdx == -1) ? "" : thiswinurl.substring(hqTaskIdIdx + (hqTaskIdStr.length + 1));

    $.ajax({
        type: "post",
        dataType: "json",
        data: {
            "hqTaskId": hqTaskIdVal
        },
        url: DI.queryTaskInfo,
        success: function(data) {
            if (data.retFlag == 1) {
                mainCtrl.hqTaskId = hqTaskIdVal;
                mainCtrl.cateName = data.data.cateName;
                mainCtrl.month = data.data.monthFormat;
                mainCtrl.taskTopic = data.data.taskTopic;
                mainCtrl.rate = data.data.rate;
            } else {
                console.log("获取数据失败了哟>>>" + data.errorMessage)
            }
        },
        error: function(err) {
            console.log(err);
        }
    });

    var opts22 = {
        targetId: "taskTable22",
        templateId: "tabletask22",
        settings: {
            searchParam: {
                "hqTaskId": hqTaskIdVal
            },
            url: DI.queryTaskModelDetail,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            }
        },
        customFuncs: {
            distributionInfo: function(modelId) {
                $("#popup_distribution").show();
                $("#load-assign").show();
                popCtrl.assignList = [];
                popCtrl.searchcode = "";
                $(".search-code .err-box").hide();
//                location.hash = "popup_distribution";
                popCtrl.modelId = modelId;
                popCtrl.hqTaskId = mainCtrl.hqTaskId;
                $.ajax({
                    type: "post",
                    dataType: "json",
                    data: {
                        "modelId": modelId,
                        "hqTaskId": mainCtrl.hqTaskId
                    },
                    url: DI.queryAreaAllocateDetail,
                    success: function(data) {
                        $("#load-assign").hide();
                        if (data.retFlag == 1) {
                            popCtrl.assignList = data.data;
                            var total = 0;
                            for (var i = 0; i < popCtrl.assignList.length; i++) {
                                var iptVal = popCtrl.assignList[i].targetNum == undefined ? 0 : popCtrl.assignList[i].targetNum;
                                total += parseInt(iptVal == "" ? 0 : iptVal, 10);
                            }
                            popCtrl.targetNum = total;
                            
                            var loop = window.setInterval(function(){
                            	if($(".targetNumIpt")){
                            		 $(".targetNumIpt").change(function(){
                                     	$(this).addClass("changeIpt");
                                     });
                            		 window.clearInterval(loop);
                            	}
                            }, 100);
                        } else {
                            console.log("获取分配详情接口，返回数据错误")
                        }
                    },
                    error: function(err) {
                        console.log(err);
                    }
                });
            },
            delPop: function(row) {
                popCtrl.hqTaskId = row.hqTaskId;
                popCtrl.modelId = row.modelId;
                $("#popup_del").show();
            }
        }
    };
    taskTable22 = SwiftUI.swiftTable.getInstance(opts22);
    
    var mainCtrl = avalon.define({
        $id: "mainController",
        taskTopic: "",
        month: "",
        categoryList: [],
        hqTaskId: "",
        cateName: "",
        isimppercent: false,
        isimpcomm: false,
        tabIdx: 0,
        rate: "",
        selTab: function() {
            mainCtrl.tabIdx = $(this).index();
        },
        importXls: function() {
            $("#popup_import").show();
            popCtrl.hqTaskId = mainCtrl.hqTaskId;
        },
        nextComm: function() {
            mainCtrl.tabIdx = 1;
        },
        prevPercent: function() {
            mainCtrl.tabIdx = 0;
        }
    });

    var popCtrl = avalon.define({
        $id: "popController",
        assignList: [],
        searchcode: "",
        delId: "",
        delcommName: "",
        modelId: "",
        hqTaskId: "",
        targetNum: 0,
        uploadFile: function() {
            if ($("#upladFile").val() == "") {
                return;
            }
            $("#load-import").show();
            $("#form_file").ajaxSubmit({
                type: "post",
                dataType: "json",
                url: DI.addModel,
                success: function(data) {
                    $("#load-import").hide();
                    if (data.retFlag == 1) {
                        $("#popup_import").hide();
                        taskTable22.searchParam.pageNo = "1";
                        taskTable22.refresh();
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                    $("#upladFile").after($("#upladFile").clone().val(""));
                    $("#upladFile").remove();
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log("ajax error");
                    console.log(XMLHttpRequest);
                }
            });
        },
        fastSearch: function() {
            var codeList = $("#popup_distribution tbody tr .region-code");
            $(".region-code.highlight").removeClass("highlight");
            $(".search-code .err-box").hide();
            if(popCtrl.searchcode.length != 5){
                $(".search-code .err-box").show();
                return;
            }
            var hashId = "";
            for (var i = 0; i < popCtrl.assignList.length; i++) {
                if (popCtrl.assignList[i].disCode.indexOf(popCtrl.searchcode) != -1) {
                    $(codeList[i]).addClass("highlight");
                    if (hashId == "") {
                        hashId = "td_" + popCtrl.assignList[i].disCode;
                    }
                }
            }
            location.hash = "";
            location.hash = hashId;
        },
        allocateModel: function() {
            if ($("#popup_distribution .u-ipt.err-ipt").length > 0) {
                return;
            }
            
            $(".targetNumIpt").each(function(){
            	var $this = $(this);
            	$this.attr("name", "areaAllocates[" + $this.parent().find('span').html() + "].targetNum");
    		});
            $(".changeIpt").each(function(){
            	var $this = $(this);
            	var $that = $this.parent().find(".areaTaskIdIpt");
            	$that.attr("name", "areaAllocates[" + $that.parent().find('span').html() + "].areaTaskId");
    		});
            
            $(".modelIdIpt").val(popCtrl.modelId);
            $(".hqTaskIdIpt").val(popCtrl.hqTaskId);
            
            $("#modelAlloForm").ajaxSubmit({
                type: "post",
                dataType: "json",
                url: DI.allocateModel,
                success: function(data) {
                	if (data.retFlag == 1) {
                        taskTable22.searchParam.pageNo = "1";
                        taskTable22.refresh();
                        PLATFORM.showTips("success", "分配成功");
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                	console.log("ajax error");
                    console.log(XMLHttpRequest);
                }
            });
            $("#popup_distribution").hide();
        },
        delData: function() {
        	$('.loadData').show();
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "modelId": popCtrl.modelId,
                    "hqTaskId": popCtrl.hqTaskId
                },
                url: DI.removeModel,
                success: function(data) {
                    $("#popup_del").hide();
                    $('.loadData').hide();
                    if (data.retFlag == 1) {
                        taskTable22.searchParam.pageNo = "1";
                        taskTable22.refresh();
                        PLATFORM.showTips("success", "删除主推款数成功");
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                    $('.loadData').hide();
                }
            });
        },
        checkNum: function() {
            $(this).removeClass("err-ipt").removeAttr("title");
            if ($(this).val() == "") {
                var total = 0;
                var iptList = $("#popup_distribution tbody .edit-ipt .targetNumIpt");
                for (var i = 0; i < iptList.length; i++) {
                    var iptVal = $(iptList[i]).val() == "" ? 0 : $(iptList[i]).val();
                    total += parseInt(iptVal, 10);
                }
                popCtrl.targetNum = total;
                return;
            }
            var reg = /^[0-9]+$/;
            if (!reg.test($(this).val())) {
                $(this).addClass("err-ipt").attr("title", "目标台数为数字");
            } else {
                var total = 0;
                var iptList = $("#popup_distribution tbody .edit-ipt .targetNumIpt");
                for (var i = 0; i < iptList.length; i++) {
                    var iptVal = $(iptList[i]).val() == "" ? 0 : $(iptList[i]).val();
                    total += parseInt(iptVal, 10);
                }
                popCtrl.targetNum = total;
            }
        }
    });

    avalon.scan();
});
/*
 * firefox下回车事件不兼容问题
 * 
 */
function fastSearch(evt){
    evt = (evt) ? evt : ((window.event) ? window.event : "");  //兼容IE和Firefox获得keyBoardEvent对象
    var keyVal = evt.keyCode?evt.keyCode:evt.which;
    $(".search-code .u-ipt").val($(".search-code .u-ipt").val().replace(/\D/g,''));
    var searchCode = $(".search-code .u-ipt").val();
    if(keyVal == 13){
        var codeList = $("#popup_distribution tbody tr .region-code");
        $(".region-code.highlight").removeClass("highlight");
        $(".search-code .err-box").hide();
        if(searchCode.length != 5){
            $(".search-code .err-box").show();
            return;
        }
        var hashId = "";
        var tableList = $("#popup_distribution tbody tr"),
            tableLen = tableList.length;
        for (var i = 0; i < tableLen; i++) {
            var curCodeObj = $(tableList[i]).find(".region-code"),
                curCode = curCodeObj.text();
            console.log(curCode);
            if (curCode.indexOf(searchCode) != -1) {
                curCodeObj.find("span").addClass("highlight");
                if (hashId == "") {
                    hashId = "td_" + curCode;
                }
            }
        }
        location.hash = "";
        location.hash = hashId;
    }
}
